package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.OrderSettinglistMapper;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Setmeal;
import com.itheima.service.OrderSettinglistService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Service(version = "1.0.0",interfaceClass = OrderSettinglistService.class)
@Transactional
public class OrderSettinglistServiceImpl implements OrderSettinglistService {

    @Resource
    private OrderSettinglistMapper orderSettinglistMapper;
    @Override
    public Result findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<HashMap<String, Object>> list = orderSettinglistMapper.findAll(queryPageBean.getQueryString());
        PageInfo<HashMap<String, Object>> pageInfo = new PageInfo<>(list);
        return Result.success(pageInfo);
    }

    @Override
    public Result isshow(Integer id) {
        orderSettinglistMapper.isshow(id);
        return Result.success(null);
    }

    @Override
    public Result noshow(Integer id) {
        orderSettinglistMapper.noshow(id);
        return Result.success(null);
    }

    @Override
    public Result findAllSetMeal() {
        List<Setmeal> list = orderSettinglistMapper.findAllSetMeal();
        return Result.success(list);
    }

    @Override
    public Result add(HashMap<String, Object> map) {
        //生成档案号
        String date_str = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        map.put("code",date_str+ UUID.randomUUID().toString().substring(0,4));
        Member member = new Member();
        member.setCode(map.get("code").toString());
        member.setName(map.get("name").toString());
        member.setIdCard(map.get("IdCard").toString());
        member.setPhoneNumber(map.get("PhoneNumber").toString());
        member.setSex(Integer.parseInt(map.get("sex").toString()));
        member.setAge(Integer.parseInt(map.get("age").toString()));
        member.setBirthday(map.get("birthday").toString());
        member.setHunyingzt(map.get("hunyingzt").toString());
        orderSettinglistMapper.add4memeber(member);
        //通过主键自增拿到刚添加的用户ID
        map.put("member_id",member.getId());
        List<Integer> ids = (List<Integer>) map.get("setMealIds");
        for (Integer id : ids) {
            map.put("setmeal_id",id);
            orderSettinglistMapper.add4order(map);
        }
        return Result.success(null);
    }
}
